//START - Drop-list which dynamically fills-in the NAMES of Primary, Joint 1, Joint 2, Joint 3, etc…
function DropListNameSelection()
{
  var A_NAME = this.getField("A_NAME");
  var CSTM_NAME = this.getField("CSTM_NAME");
  var prevName = "";
	  var dropListArr = [["Select Member Name","[None]"]];//Array containing the values of the droplist to be set later																											   
  var checkNames = ["Select Member Name"];//Makes sure all values in drop list are unique

  //If the drop list has a value from being processed again, prevName will store the value
  if (CSTM_NAME.value != ""){
    prevName = CSTM_NAME.value;
  }

  if (A_NAME.value != ""){
	dropListArr.push([A_NAME.value, A_NAME.value]);//Add the primary to the droplist array
    checkNames.push(A_NAME.value);
  }

  //The number 4 must be updated depending on how many JNi_NAME mapped on the document.
  for (var i = 1; i <= 4; i++) {
    if (this.getField("JN" + i + "_NAME").value != "" && checkNames.indexOf(this.getField("JN" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
      CSTM_NAME.insertItemAt(this.getField("JN" + i + "_NAME").value, this.getField("JN" + i + "_NAME").value, -1);//Inserts name at the end of the drop list
      checkNames.push(this.getField("JN" + i + "_NAME").value);
    }
  }

  //The number 4 must be updated depending on how many ASi_NAME mapped on the document.
  for (var i = 1; i <= 4; i++)  {
    if (this.getField("AS" + i + "_NAME").value != ""  && checkNames.indexOf(this.getField("AS" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
      CSTM_NAME.insertItemAt(this.getField("AS" + i + "_NAME").value, this.getField("AS" + i + "_NAME").value, -1);//Inserts name at the end of the drop list
      checkNames.push(this.getField("AS" + i + "_NAME").value);
    }
  }
    CSTM_NAME.setItems(dropListArr);//Sets the values inside dropListArr to the droplist itself																							   
    //Retains the drop list value when reprocessed 
    if (prevName != "" && prevName != "Select Member Name" && prevName != "[None]"){
      CSTM_NAME.value = prevName;
    }else{
      CSTM_NAME.value = "Select Member Name";
    }
}
//END - Drop-list which dynamically fills-in the NAMES of Primary, Joint 1, Joint 2, Joint 3, etc…

//START - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names
function FirstMemberSelected()
{
  var CSTM_NAME = this.getField("CSTM_NAME");
  var CSTM_SIG_NAME = this.getField("CSTM_SIG_NAME");
  var TEAWEB_SIG_A_1_TXT = this.getField("TEAWEB_SIG_A_1_TXT");
  
  TEAWEB_SIG_A_1_TXT.value = 0;
  this.getField("TEAWEB_SIG_JN1_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_JN2_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_JN3_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_JN4_1_TXT").value = 0;

  this.getField("TEAWEB_SIG_AS1_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_AS2_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_AS3_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_AS4_1_TXT").value = 0;

  this.getField("TEAWEB_SIG_NM1_1_TXT").value = 0;

  CSTM_SIG_NAME.value = "";
  
  
  if (CSTM_NAME.value == this.getField("A_NAME").value) {
   		
  		TEAWEB_SIG_A_1_TXT.value = 1;
  		CSTM_SIG_NAME.value = this.getField("A_NAME").value;
  	  
  }
  else {
        var signerMatched = false;

  			for (var i = 1; i <= 4; i++) {
  			  if (CSTM_NAME.value == this.getField("JN" + i + "_NAME").value) {
  				this.getField("TEAWEB_SIG_JN" + i + "_1_TXT").value = 1;
  				CSTM_SIG_NAME.value = this.getField("JN" + i + "_NAME").value;
          signerMatched = true;
          return;//If the value is set exit the function so no additional TXT field gets enabled.
  			  }
  			}

  			for (var i = 1; i <= 4; i++) {
  			  if (CSTM_NAME.value == this.getField("AS" + i + "_NAME").value) {
  				this.getField("TEAWEB_SIG_AS" + i + "_1_TXT").value = 1;
  				CSTM_SIG_NAME.value = this.getField("AS" + i + "_NAME").value;
          signerMatched = true;
          return;//If the value is set exit the function so no additional TXT field gets enabled.
  			  }
  			}

			if (signerMatched == false && CSTM_NAME.value != "Select Member Name" && CSTM_NAME.value != "[None]") 
			{
			  this.getField("NM1_NAME").value = CSTM_NAME.value;
			  CSTM_SIG_NAME.value = this.getField("NM1_NAME").value;
			  this.getField("TEAWEB_SIG_NM1_1_TXT").value = 1;  
			}
		}
}

//The two events below are needed for the Drop-List to ensure Validate button will be required if the Drop List Selection is changed when "Process Sessions in All Browsers" is checked.
function CSTM_NAME_PDFBlur()
{
  FirstMemberSelected();
}

function CSTM_NAME_PDFMouseDown()
{
  this.getField("VALIDATE_SIGNERS_FIELD").value = ""; //Clears the name selection validation if the user chooses a different name.
}

function VALIDATE_SIGNERS_BTN_PDFMouseUp()
{
  FirstMemberSelected();
  this.getField("VALIDATE_SIGNERS_FIELD").value = "VALIDATED";
}
//END - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names